package com.sdcqjy.property.presenter;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sdcqjy.mylibrary.http.BaseRequestBack;
import com.sdcqjy.mylibrary.http.BaseRet;
import com.sdcqjy.mylibrary.http.HttpClient;
import com.sdcqjy.property.base.BaseContract;
import com.sdcqjy.property.mode.LawRegulationsDetailsMode;
import com.sdcqjy.property.mode.LawRegulationsMode;
import com.sdcqjy.property.presenter.contract.LawRegulationsContract;
import com.sdcqjy.property.presenter.contract.a;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 磁磁帅 on 2018/7/4.
 * 功能：
 */

public class LawRegulationsPresenter extends BaseContract.Presenter<LawRegulationsContract.View> {
    public LawRegulationsPresenter(LawRegulationsContract.View baseView) {
        super(baseView);
    }

    public void getList(String searchTxt) {
        Map<String, Object> map = new HashMap<>();
        if (!TextUtils.isEmpty(searchTxt)) map.put("title", searchTxt);
        new HttpClient().setHttpType(HttpClient.HttpType.JSON)
                .setParam(map)
                .httpRequest(a.LawRegulations, new BaseRequestBack<BaseRet<List<LawRegulationsMode>>>() {
                    public BaseRet<List<LawRegulationsMode>> jsonToMode(String jsonStr) {
                        BaseRet<List<LawRegulationsMode>> ret = new Gson().fromJson(jsonStr, new TypeToken<BaseRet<List<LawRegulationsMode>>>() {
                        }.getType());
                        return ret;
                    }

                    public void onResponseSucc(BaseRet<List<LawRegulationsMode>> ret) {
                        if (baseView != null) {
                            //baseView.dismissDialog();
                            if (ret.isOk()) {
                                baseView.getListRet(ret.data);
                            } else {
                                baseView.applyError(ret.msg);
                            }
                        }
                    }

                    public void onResponseFail(String retStr) {
                        if (baseView != null) {
                            //baseView.dismissDialog();
                            baseView.applyError(retStr);
                        }
                    }
                });
        //if (baseView != null) baseView.openLoadDialog();
    }

    public void getList2(LawRegulationsMode mode) {
        new HttpClient().setHttpType(HttpClient.HttpType.GET)
                .addParam("id", mode.id)
                .httpRequest(a.LawRegulations2, new BaseRequestBack<BaseRet<List<LawRegulationsMode>>>() {
                    public BaseRet<List<LawRegulationsMode>> jsonToMode(String jsonStr) {
                        BaseRet<List<LawRegulationsMode>> ret = new Gson().fromJson(jsonStr, new TypeToken<BaseRet<List<LawRegulationsMode>>>() {
                        }.getType());
                        return ret;
                    }

                    public void onResponseSucc(BaseRet<List<LawRegulationsMode>> ret) {
                        if (baseView != null) {
                            baseView.dismissDialog();
                            if (ret.isOk()) {
                                List<LawRegulationsMode> data = ret.data;
                                mode.setModeList(data);
                                baseView.getList2Ret(mode);
                            } else {
                                baseView.applyError(ret.msg);
                            }
                        }
                    }

                    public void onResponseFail(String retStr) {
                        if (baseView != null) {
                            baseView.dismissDialog();
                            baseView.applyError(retStr);
                        }
                    }
                });
        if (baseView != null) baseView.openLoadDialog();
    }

    public void getListDetails(String id) {
        new HttpClient().setHttpType(HttpClient.HttpType.GET)
                .addParam("id", id)
                .httpRequest(a.LawRegulationsDetails, new BaseRequestBack<BaseRet<LawRegulationsDetailsMode>>() {
                    public BaseRet<LawRegulationsDetailsMode> jsonToMode(String jsonStr) {
                        BaseRet<LawRegulationsDetailsMode> baseRet = new BaseRet<>();
                        BaseRet<List<LawRegulationsDetailsMode>> ret = new Gson().fromJson(jsonStr, new TypeToken<BaseRet<List<LawRegulationsDetailsMode>>>() {
                        }.getType());
                        baseRet.success = ret.success;
                        baseRet.code = ret.code;
                        baseRet.msg = ret.msg;
                        if (ret.success) {
                            List<LawRegulationsDetailsMode> data = ret.data;
                            if (data.size() > 0) {
                                LawRegulationsDetailsMode lawRegulationsDetailsMode = data.get(0);
                                baseRet.data = lawRegulationsDetailsMode;
                            }
                        }
                        return baseRet;
                    }

                    public void onResponseSucc(BaseRet<LawRegulationsDetailsMode> ret) {
                        if (baseView != null) {
                            baseView.dismissDialog();
                            if (ret.isOk()) {
                                baseView.getListDetailsRet(ret.data);
                            } else {
                                baseView.applyError(ret.msg);
                            }
                        }
                    }

                    public void onResponseFail(String retStr) {
                        if (baseView != null) {
                            baseView.dismissDialog();
                            baseView.applyError(retStr);
                        }
                    }
                });
        if (baseView != null) baseView.openLoadDialog();
    }
}
